package com.md.album.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.md.album.R
import com.md.album.SelfWrapperAlbum
import com.md.album.db.SelfAlbumManager
import com.md.album.ui.*
import com.md.network.api.Audio

/**
 * @author liyue
 * create 2021/4/5
 * desc 自建专辑列表adapter
 */
class SelfCreateAlbumAdapter(context: Context,itemClickListener: OnItemClickListener, albumList:List<SelfWrapperAlbum>) :RecyclerView.Adapter<SelfCreateAlbumAdapter.AlbumViewHolder>(){

    private var mContext:Context = context
    private var albumList:List<SelfWrapperAlbum> =albumList
    private var listener:OnItemClickListener = itemClickListener

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
        var view = LayoutInflater.from(mContext).inflate(R.layout.adapter_selfcreated_album,parent,false)
        return AlbumViewHolder(view)
    }

    override fun getItemCount(): Int {
       return albumList.size
    }

    override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
        var curAlbum = albumList[position]
        holder.tvTitle?.text= curAlbum.album.name
        holder.tvTotalSize?.text= curAlbum.totalCount.toString()
        holder.cltItemRoot!!.setOnClickListener{
            listener?.let {
                var audioList = ArrayList<Audio>()
                var audios = SelfAlbumManager.getIns().getAudiosByAlbumId(curAlbum.album.id)
                audioList.addAll(audios)
                listener.onItemClick(CREATED_ALBUM,curAlbum.album.name,audioList)
            }
        }
    }

    public fun setData(albumList:List<SelfWrapperAlbum>){
        this.albumList= albumList
        notifyDataSetChanged()
    }


    class AlbumViewHolder: RecyclerView.ViewHolder{
        var cltItemRoot : ConstraintLayout?= null
        var ivCover: ImageView?= null
        var tvTitle: TextView?= null
        var tvTotalSize: TextView?= null
        var ivNext: ImageView?= null
        constructor(itemView:View):super(itemView){
            cltItemRoot = itemView.findViewById(R.id.cltItemRoot)
            ivCover = itemView.findViewById(R.id.ivCover)
            tvTitle = itemView.findViewById(R.id.tvTitle)
            tvTotalSize = itemView.findViewById(R.id.tvTotalSize)
            ivNext = itemView.findViewById(R.id.ivNext)
        }
    }

    interface OnItemClickListener{
        fun onItemClick(albumType:Int , curAlbumName:String, audioList:ArrayList<Audio>)
    }
}